package com.umeox.zikrringtest

import android.Manifest
import android.annotation.SuppressLint
import android.bluetooth.BluetoothAdapter
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.location.LocationManager
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.provider.Settings
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatButton
import androidx.core.content.ContextCompat

@SuppressLint("CustomSplashScreen")
class SplashActivity: AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)

        findViewById<AppCompatButton>(R.id.bt).setOnClickListener {
            val requestPermissions = mutableListOf<String>()
            //蓝牙权限
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                requestPermissions.add(Manifest.permission.BLUETOOTH_CONNECT)
                requestPermissions.add(Manifest.permission.BLUETOOTH_SCAN)
            } else {
                requestPermissions.add(Manifest.permission.ACCESS_COARSE_LOCATION)
                requestPermissions.add(Manifest.permission.ACCESS_FINE_LOCATION)
            }

            //存储权限
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                requestPermissions.add(Manifest.permission.READ_MEDIA_VIDEO)
                requestPermissions.add(Manifest.permission.READ_MEDIA_IMAGES)
                requestPermissions.add(Manifest.permission.READ_MEDIA_AUDIO)
            } else {
                requestPermissions.add(Manifest.permission.READ_EXTERNAL_STORAGE)
                requestPermissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE)
            }

            var requestResult = true
            for (requestPermission in requestPermissions) {
                if (!checkPermissionExt(requestPermission)) {
                    requestResult = false
                }
            }
            if (requestResult) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                    if (!Environment.isExternalStorageManager()) {
                        startActivity(Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION))
                    } else {
                        intoNext()
                    }
                } else {
                    intoNext()
                }

            } else {
                requestPermissions(requestPermissions.toTypedArray(),1000)
            }
        }
    }



    private fun intoNext() {
        //检查蓝牙开关
        if (!isEnableBluetooth()) {
            Toast.makeText(this, "请开打蓝牙开关后再试", Toast.LENGTH_SHORT).show()
            enableBluetooth()
            return
        }

        //检查GPS状态
        if (!gpsIsEnable(this)) {
            Toast.makeText(this, "请开打GPS开关后再试", Toast.LENGTH_SHORT).show()
            return
        }

        startActivity(Intent(this,MainActivity::class.java))
        finish()
    }

    private fun isEnableBluetooth(): Boolean {
        val adapter = BluetoothAdapter.getDefaultAdapter()
        return adapter.isEnabled
    }

    @SuppressLint("MissingPermission")
    private fun enableBluetooth(): Boolean {
        val adapter = BluetoothAdapter.getDefaultAdapter()
        return adapter.enable()
    }

    private fun gpsIsEnable(context: Context): Boolean {
        val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
        return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
                locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
    }

    private fun checkPermissionExt(permission: String): Boolean {
        return ContextCompat.checkSelfPermission(
            this,
            permission
        ) == PackageManager.PERMISSION_GRANTED
    }

}